using System;
using System.Data;

using System.Configuration;

using System.Data.OleDb;
using Entity;

namespace SalesVMT
{
    public class ShipmentDetailsDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ShipmentDetails_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ShipmentDetails_GetList", dbConn);
            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static DataSet GetAllByShipment(int ShipmentID)
        {
            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ShipmentDetails_GetAllByShipment", dbConn);
            dbCmd.Parameters.AddWithValue("@Shipment_ID", ShipmentID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Delete(int _Shipment_ID)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ShipmentDetails_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Shipment_ID", _Shipment_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(ShipmentDetailsInfo _ShipmentDetailsInfo)
        {
            int retVal = 0;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ShipmentDetails_Insert", dbConn);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_ItemID", _ShipmentDetailsInfo.ShipmentDetails_ItemID);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_ShipmentID", _ShipmentDetailsInfo.ShipmentDetails_ShipmentID);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_Quantity", _ShipmentDetailsInfo.ShipmentDetails_Quantity);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_Price", _ShipmentDetailsInfo.ShipmentDetails_Price);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_DownPrice", _ShipmentDetailsInfo.ShipmentDetails_DownPrice);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_VAT", _ShipmentDetailsInfo.ShipmentDetails_VAT);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_Total", _ShipmentDetailsInfo.ShipmentDetails_Total);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static ShipmentDetailsInfo GetInfo(int _ShipmentDetails_ID)
        {
            ShipmentDetailsInfo retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ShipmentDetails_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@ShipmentDetails_ID", _ShipmentDetails_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            OleDbDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ShipmentDetailsInfo();
                    retVal.ShipmentDetails_ID = Convert.ToInt32(dr["ShipmentDetails_ID"]);
                    retVal.ShipmentDetails_ItemID = Convert.ToInt32(dr["ShipmentDetails_ItemID"]);
                    retVal.ShipmentDetails_ShipmentID = Convert.ToInt32(dr["ShipmentDetails_ShipmentID"]);
                    retVal.ShipmentDetails_Quantity = Convert.ToInt32(dr["ShipmentDetails_Quantity"]);
                    retVal.ShipmentDetails_Price = Convert.ToDecimal(dr["ShipmentDetails_Price"]);
                    retVal.ShipmentDetails_DownPrice = Convert.ToDecimal(dr["ShipmentDetails_DownPrice"]);
                    retVal.ShipmentDetails_VAT = Convert.ToInt32(dr["ShipmentDetails_VAT"]);
                    retVal.ShipmentDetails_Total = Convert.ToDecimal(dr["ShipmentDetails_Total"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}
